<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="utf-8" />
    
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" />
  <title>
    Java-Stream流 |  Yolin
  </title>
  <meta name="generator" content="hexo-theme-yilia-plus">
  
  <link rel="shortcut icon" href="/favicon.png" />
  
  
<link rel="stylesheet" href="/css/style.css">

  
<script src="/js/pace.min.js"></script>


  

  

<link rel="alternate" href="/atom.xml" title="Yolin" type="application/atom+xml">
</head>

</html>

<body>
  <div id="app">
    <main class="content">
      <section class="outer">
  <article id="post-Java-Stream流" class="article article-type-post" itemscope
  itemprop="blogPost" data-scroll-reveal>

  <div class="article-inner">
    
    <header class="article-header">
       
<h1 class="article-title sea-center" style="border-left:0" itemprop="name">
  Java-Stream流
</h1>
  

    </header>
    

    
    <div class="article-meta">
      <a href="/2020/08/30/Java-Stream%E6%B5%81/" class="article-date">
  <time datetime="2020-08-30T09:58:02.000Z" itemprop="datePublished">2020-08-30</time>
</a>
      
  <div class="article-category">
    <a class="article-category-link" href="/categories/Java/">Java</a>
  </div>

      
      
<div class="word_count">
    <span class="post-time">
        <span class="post-meta-item-icon">
            <i class="ri-quill-pen-line"></i>
            <span class="post-meta-item-text"> 字数统计:</span>
            <span class="post-count">1.5k字</span>
        </span>
    </span>

    <span class="post-time">
        &nbsp; | &nbsp;
        <span class="post-meta-item-icon">
            <i class="ri-book-open-line"></i>
            <span class="post-meta-item-text"> 阅读时长≈</span>
            <span class="post-count">6分钟</span>
        </span>
    </span>
</div>

      
    </div>
    

    
    
    <div class="tocbot"></div>





    

    <div class="article-entry" itemprop="articleBody">
      


      

      
      <h2 id="一、体验Stream流"><a href="#一、体验Stream流" class="headerlink" title="一、体验Stream流"></a>一、体验Stream流</h2><p>需求：</p>
<ol>
<li>创建一个集合，存储多个字符串元素</li>
<li>把集合中所有以“张”开头的元素存储到一个新的集合</li>
<li>把“张”开头的集合中的长度为3的元素存储到新的集合</li>
<li>遍历上一步得到的集合</li>
</ol>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-29_10-10-35.jpg" alt=""></p>
<p>而这样内容太复杂了，想使代码更简洁就需要用到接下来要学习的Stream流</p>
<a id="more"></a>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-29_10-25-36.jpg" alt=""></p>
<h2 id="二、Stream流的常见生成方式"><a href="#二、Stream流的常见生成方式" class="headerlink" title="二、Stream流的常见生成方式"></a>二、Stream流的常见生成方式</h2><h3 id="2-1-Stream流的使用"><a href="#2-1-Stream流的使用" class="headerlink" title="2.1 Stream流的使用"></a>2.1 Stream流的使用</h3><h4 id="生成流"><a href="#生成流" class="headerlink" title="生成流"></a>生成流</h4><blockquote>
<p>通过数据源(集合，数组等)生成流 </p>
<p>list.stream()</p>
</blockquote>
<h4 id="中间操作"><a href="#中间操作" class="headerlink" title="中间操作"></a>中间操作</h4><blockquote>
<p>一个流后面可以跟随零个或多个中间操作，其目的主要是打开流，做出某种程度的数据过滤/映射，然后返回一个新的流，交给下一个操作使用</p>
<p>filter()</p>
</blockquote>
<h4 id="终结操作"><a href="#终结操作" class="headerlink" title="终结操作"></a>终结操作</h4><blockquote>
<p>一个流只能有一个终结操作，当这个操作执行后，流就被使用“光”了，无法再被操作。所以这必定是流的最后一个操作</p>
<p>forEach()</p>
</blockquote>
<h3 id="2-2-Stream流的常见生成方式"><a href="#2-2-Stream流的常见生成方式" class="headerlink" title="2.2 Stream流的常见生成方式"></a>2.2 Stream流的常见生成方式</h3><ul>
<li>Collection体系的集合可以使用默认方法stream()生成流，<strong>default Stream<E> stream()</strong></li>
<li>Map体系的集合间接的生成流</li>
<li>数组可以通过Stream接口的静态方法of(T…values)生成流</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StreamDemo3</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//Collection体系的集合可以使用默认方法stream()生成流</span></span><br><span class="line">        List&lt;String&gt; list=<span class="keyword">new</span> ArrayList&lt;String&gt;();</span><br><span class="line">        Stream&lt;String&gt; stream = list.stream();</span><br><span class="line"></span><br><span class="line">        Set&lt;String&gt; set=<span class="keyword">new</span> HashSet&lt;String&gt;();</span><br><span class="line">        Stream&lt;String&gt; setStream = set.stream();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//Map体系的集合间接的生成流</span></span><br><span class="line">        Map&lt;String,Integer&gt; map=<span class="keyword">new</span> HashMap&lt;String,Integer&gt;();</span><br><span class="line">        Stream&lt;String&gt; keyStream = map.keySet().stream();</span><br><span class="line">        Stream&lt;Integer&gt; valueStream=map.values().stream();</span><br><span class="line">        <span class="comment">//键值对对象对应的流</span></span><br><span class="line">        Stream&lt;Map.Entry&lt;String, Integer&gt;&gt; entryStream = map.entrySet().stream();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//数组可以通过Stream接口的静态方法生成流</span></span><br><span class="line">        String[] strArr=&#123;<span class="string">"hello"</span>,<span class="string">"world"</span>,<span class="string">"yolin"</span>&#125;;</span><br><span class="line">        Stream&lt;String&gt; sstrArrayStream1 = Stream.of(strArr);</span><br><span class="line">        Stream&lt;String&gt; strArrayStream2 = Stream.of(<span class="string">"hello"</span>, <span class="string">"world"</span>, <span class="string">"yolin"</span>);</span><br><span class="line">        Stream&lt;Integer&gt; integerStream = Stream.of(<span class="number">10</span>, <span class="number">20</span>, <span class="number">30</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h3 id="2-3-Stream流的常见中间操作方式"><a href="#2-3-Stream流的常见中间操作方式" class="headerlink" title="2.3 Stream流的常见中间操作方式"></a>2.3 Stream流的常见中间操作方式</h3><p><strong>注意以下方法都在Stream接口中</strong></p>
<h4 id="操作方式一"><a href="#操作方式一" class="headerlink" title="操作方式一"></a>操作方式一</h4><p><strong>Stream<T> filter(Predicate predicate)</strong>:用于对流中的数据进行过滤</p>
<p>Predicate接口中的方法</p>
<p>boolean test(T t)：对给定的参数进行判断，返回一个布尔值</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-29_17-00-46.jpg" alt=""></p>
<h4 id="操作方式二"><a href="#操作方式二" class="headerlink" title="操作方式二"></a>操作方式二</h4><p><strong>Stream<T> limit(long maxSize)</strong>:返回此流中的元素组成的流，截取前指定参数个数的数据</p>
<p><strong>Stream<T> skip(long n)</strong>:跳过指定参数个数的数据，返回由该流的剩余元素组成的流</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-29_18-09-02.jpg" alt=""></p>
<h4 id="操作方式三"><a href="#操作方式三" class="headerlink" title="操作方式三"></a>操作方式三</h4><p><strong>static<T> Stream<T> concat(Stream a,Stream b)</strong>:合并a和b两个流为一个流</p>
<p><strong>Stream<T> distinct()</strong>:返回由该流的不同元素(根据Object.equals(Object))组成的流</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-29_18-32-16.jpg" alt=""></p>
<h4 id="操作方式四"><a href="#操作方式四" class="headerlink" title="操作方式四"></a>操作方式四</h4><p><strong>Stream<T> sorted()</strong>:返回由此流的元素组成的流，根据自然顺序排序</p>
<p><strong>Stream<T> sorted(Comparator comparator)</strong>:返回由该流的元素组成的流，根据提供的Comparator进行排序</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-29_21-32-36.jpg" alt=""></p>
<h4 id="操作方式五"><a href="#操作方式五" class="headerlink" title="操作方式五"></a>操作方式五</h4><p><strong><R> Stream<R> map(Function mapper)</strong>:返回由给定函数应用于此流的元素的结果组成的流</p>
<blockquote>
<p>Function接口中的方法  <strong>R apply(T t)</strong></p>
</blockquote>
<p><strong>IntStream mapToInt(ToIntFunction mapper)</strong>:返回一个IntStream其中包含将给定函数应用于此流的元素的结果。</p>
<blockquote>
<p>IntStream:表示原始int流</p>
<p>ToIntFunction 接口中的方法  <strong>int applyAsInt(T value)</strong></p>
</blockquote>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-30_14-35-14.jpg" alt=""></p>
<h3 id="2-4-Stream流的常见终结操作方式"><a href="#2-4-Stream流的常见终结操作方式" class="headerlink" title="2.4 Stream流的常见终结操作方式"></a>2.4 Stream流的常见终结操作方式</h3><p><strong>void forEach(Consumer action)</strong>:对此流的每个元素执行操作</p>
<blockquote>
<p>Consumer接口中的方法  void accept(T t):对给定的参数执行此操作</p>
</blockquote>
<p><strong>long count()</strong>:返回此流中的元素数</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-30_14-43-25.jpg" alt=""></p>
<h2 id="三、Stream流练习"><a href="#三、Stream流练习" class="headerlink" title="三、Stream流练习"></a>三、Stream流练习</h2><p>现有两个ArrayList集合，分别存储6名男演员名称和6名女演员名称，要求完成如下操作</p>
<ul>
<li>男演员只要名字为3个字的前三个人</li>
<li>女演员只要姓林的，并且不要第一个</li>
<li>把过滤后的男演员姓名和女演员姓名合并到一起</li>
<li>把上一步操作后的元素作为构造方法的参数创建演员对象，遍历数据</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-30_16-02-05.jpg" alt=""></p>
<p>可以使用一步完成</p>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-30_16-03-21.jpg" alt=""></p>
<h2 id="四、Stream流的收集操作"><a href="#四、Stream流的收集操作" class="headerlink" title="四、Stream流的收集操作"></a>四、Stream流的收集操作</h2><p>‘对数据使用Stream流的方式操作完毕后，需要将流中的数据收集到集合中，如何操作？</p>
<p>Stream流的收集方法</p>
<ul>
<li><strong>R collect(Collector collector)</strong></li>
<li>但这个收集方法的参数是一个Collector接口</li>
</ul>
<p>工具类Collectors提供了具体的收集方式</p>
<ul>
<li><strong>public static <T> Collector toList()</strong>:把元素收集到List集合中</li>
<li><strong>public static <T> Collector toSet()</strong>:把元素收集到Set集合中</li>
<li><strong>public static Collector toMap(Function keyMappe,Function valueMapper)</strong>:把元素收集到Map集合中</li>
</ul>
<p><img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/Snipaste_2020-08-30_17-13-03.jpg" alt=""></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">StreamTo</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//创建List集合对象</span></span><br><span class="line">        List&lt;String&gt; list=<span class="keyword">new</span> ArrayList&lt;String&gt;();</span><br><span class="line">        list.add(<span class="string">"林青霞"</span>);</span><br><span class="line">        list.add(<span class="string">"张曼玉"</span>);</span><br><span class="line">        list.add(<span class="string">"柳岩"</span>);</span><br><span class="line">        <span class="comment">//1.得到名字为3个字的流</span></span><br><span class="line">        Stream&lt;String&gt; listStream = list.stream().filter(s -&gt; s.length() == <span class="number">3</span>);</span><br><span class="line">        <span class="comment">//2.把使用stream流操作完毕的数据收集到List集合中并遍历</span></span><br><span class="line">        List&lt;String&gt; names = listStream.collect(Collectors.toList());</span><br><span class="line">        <span class="keyword">for</span> (String s:names)&#123;</span><br><span class="line">            System.out.println(s);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建Set集合对象</span></span><br><span class="line">        Set&lt;Integer&gt; set=<span class="keyword">new</span> HashSet&lt;Integer&gt;();</span><br><span class="line">        set.add(<span class="number">10</span>);</span><br><span class="line">        set.add(<span class="number">20</span>);</span><br><span class="line">        set.add(<span class="number">30</span>);</span><br><span class="line">        set.add(<span class="number">33</span>);</span><br><span class="line">        set.add(<span class="number">35</span>);</span><br><span class="line">        <span class="comment">//3.得到年龄大于25 的流</span></span><br><span class="line">        Stream&lt;Integer&gt; setStream = set.stream().filter(i -&gt; i &gt; <span class="number">25</span>);</span><br><span class="line">        <span class="comment">//4.把使用Stream流操作完毕的数据收集到Set集合并遍历</span></span><br><span class="line">        Set&lt;Integer&gt; collect = setStream.collect(Collectors.toSet());</span><br><span class="line">        <span class="keyword">for</span>(Integer i:collect)&#123;</span><br><span class="line">            System.out.println(i);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建字符串数组</span></span><br><span class="line">        String[] strArray=&#123;<span class="string">"猪头新,20"</span>,<span class="string">"张望,28"</span>,<span class="string">"刘升,30"</span>,<span class="string">"王宇,25"</span>&#125;;</span><br><span class="line">        <span class="comment">//5.得到字符串中年龄大于25的流</span></span><br><span class="line">        Stream&lt;String&gt; stringStream = Stream.of(strArray).filter(s -&gt; Integer.parseInt(s.split(<span class="string">","</span>)[<span class="number">1</span>]) &gt; <span class="number">25</span>);</span><br><span class="line">        <span class="comment">//6.把使用Stream流操作完毕的数据收集到Map集合中并遍历，字符串中的姓名作键，年龄作值</span></span><br><span class="line">        Map&lt;String, Integer&gt; map = stringStream.collect(Collectors.toMap(s -&gt; s.split(<span class="string">","</span>)[<span class="number">0</span>], s -&gt; Integer.parseInt(s.split(<span class="string">","</span>)[<span class="number">1</span>])));</span><br><span class="line">        Set&lt;String&gt; keySet = map.keySet();</span><br><span class="line">        <span class="keyword">for</span>(String key:keySet)&#123;</span><br><span class="line">            Integer value = map.get(key);</span><br><span class="line">            System.out.println(key+<span class="string">","</span>+value);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
      
      <!-- reward -->
      
    </div>
      <!-- copyright -->
      
    <footer class="article-footer">
      
      
  <ul class="article-tag-list" itemprop="keywords"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/Stream%E6%B5%81/" rel="tag">Stream流</a></li></ul>


    </footer>

  </div>

  
  
  <nav class="article-nav">
    
      <a href="/2020/09/05/Java-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E5%99%A8%E4%B8%8E%E5%8F%8D%E5%B0%84/" class="article-nav-link">
        <strong class="article-nav-caption">上一篇</strong>
        <div class="article-nav-title">
          
            Java-类加载器与反射
          
        </div>
      </a>
    
    
      <a href="/2020/08/28/Java-%E5%87%BD%E6%95%B0%E5%BC%8F%E6%8E%A5%E5%8F%A3/" class="article-nav-link">
        <strong class="article-nav-caption">下一篇</strong>
        <div class="article-nav-title">Java-函数式接口</div>
      </a>
    
  </nav>


  

  
  
<!-- valine评论 -->
<div id="vcomments-box">
    <div id="vcomments">
    </div>
</div>
<script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src='https://cdn.jsdelivr.net/npm/valine@1.3.10/dist/Valine.min.js'></script>
<script>
    new Valine({
        el: '#vcomments',
        notify: false,
        verify: '',
        app_id: '',
        app_key: '',
        path: window.location.pathname,
        avatar: 'mp',
        placeholder: '给我的文章加点评论吧~',
        recordIP: true
    });
    const infoEle = document.querySelector('#vcomments .info');
    if (infoEle && infoEle.childNodes && infoEle.childNodes.length > 0) {
        infoEle.childNodes.forEach(function (item) {
            item.parentNode.removeChild(item);
        });
    }
</script>
<style>
    #vcomments-box {
        padding: 5px 30px;
    }

    @media screen and (max-width: 800px) {
        #vcomments-box {
            padding: 5px 0px;
        }
    }

    #vcomments-box #vcomments {
        background-color: #fff;
    }

    .v .vlist .vcard .vh {
        padding-right: 20px;
    }

    .v .vlist .vcard {
        padding-left: 10px;
    }
</style>

  

  
  
  

</article>
</section>
      <footer class="footer">
  <div class="outer">
    <ul class="list-inline">
      <li>
        &copy;
        2020-2021
        张金
      </li>
      <li>
        
      </li>
    </ul>
    <ul class="list-inline">
      <li>
        
        
        <span>
  <i>PV:<span id="busuanzi_value_page_pv"></span></i>
  <i>UV:<span id="busuanzi_value_site_uv"></span></i>
</span>
        
      </li>
      <li>
        <!-- cnzz统计 -->
        
        <script type="text/javascript" src='https://s9.cnzz.com/z_stat.php?id=1278069914&amp;web_id=1278069914'></script>
        
        <img src="https://yolin121.oss-cn-beijing.aliyuncs.com/blogimg/beian.png">
        <a href="https://beian.miit.gov.cn"  style="color:#f72b07" target="_blank">陕ICP备20002449号</a>
      </li>

    </ul>
  </div>
</footer>
    <div class="to_top">
        <div class="totop" id="totop">
  <i class="ri-arrow-up-line"></i>
</div>
      </div>
    </main>
      <aside class="sidebar">
        <button class="navbar-toggle"></button>
<nav class="navbar">
  
  <div class="logo">
    <a href="/"><img src="/images/ayer-side.svg" alt="Yolin"></a>
  </div>
  
  <ul class="nav nav-main">
    
    <li class="nav-item">
      <a class="nav-item-link" href="/">主页</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/archives">归档</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/categories">分类</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/tags">标签</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/playlist">歌单</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/video">视频</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/about">关于我</a>
    </li>
    
    <li class="nav-item">
      <a class="nav-item-link" href="/friends">友情链接</a>
    </li>
    
  </ul>
</nav>
<nav class="navbar navbar-bottom">
  <ul class="nav">
    <li class="nav-item">
      
      <a class="nav-item-link nav-item-search"  title="Search">
        <i class="ri-search-line"></i>
      </a>
      
      
      <a class="nav-item-link" target="_blank" href="/atom.xml" title="RSS Feed">
        <i class="ri-rss-line"></i>
      </a>
      
    </li>
  </ul>
</nav>
<div class="search-form-wrap">
  <div class="local-search local-search-plugin">
  <input type="search" id="local-search-input" class="local-search-input" placeholder="Search...">
  <div id="local-search-result" class="local-search-result"></div>
</div>
</div>
      </aside>
      <div id="mask"></div>

<!-- #reward -->
<div id="reward">
  <span class="close"><i class="ri-close-line"></i></span>
  <p class="reward-p"><i class="ri-cup-line"></i>请我喝杯咖啡吧~</p>
  <div class="reward-box">
    
    <div class="reward-item">
      <img class="reward-img" src="/images/alipay.jpg">
      <span class="reward-type">支付宝</span>
    </div>
    
    
    <div class="reward-item">
      <img class="reward-img" src="/images/wechat.jpg">
      <span class="reward-type">微信</span>
    </div>
    
  </div>
</div>
      
<script src="/js/jquery-2.0.3.min.js"></script>


<script src="/js/jquery.justifiedGallery.min.js"></script>


<script src="/js/lazyload.min.js"></script>


<script src="/js/busuanzi-2.3.pure.min.js"></script>


<script src="/js/share.js"></script>




<script>
  try {
    var typed = new Typed("#subtitle", {
    strings: ['肠断月明红豆蔻,','月似当时,','人似当时否？'],
    startDelay: 1500,
    typeSpeed: 200,
    loop: true,
    backSpeed: 40,
    showCursor: true
    });
  } catch (err) {
  }
  
</script>




<script src="/js/tocbot.min.js"></script>

<script>
  // Tocbot_v4.7.0  http://tscanlin.github.io/tocbot/
  tocbot.init({
    tocSelector: '.tocbot',
    contentSelector: '.article-entry',
    headingSelector: 'h1, h2, h3, h4, h5, h6',
    hasInnerContainers: true,
    scrollSmooth: true,
    scrollContainer:'main',
    positionFixedSelector: '.tocbot',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
    onClick: (e) => {
      $('.toc-link').removeClass('is-active-link');
      $(`a[href=${e.target.hash}]`).addClass('is-active-link');
      $(e.target.hash).scrollIntoView();
      return false;
    }
  });
</script>


<script>
  var ayerConfig = {
    mathjax: true
  }
</script>


<script src="/js/ayer.js"></script>


<script src="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-modal@0.9.2/jquery.modal.min.css">


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">

    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>

    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">

        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>

        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">

            <div class="pswp__top-bar">

                <!--  Controls are self-explanatory. Order can be changed. -->

                <div class="pswp__counter"></div>

                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>

                <button class="pswp__button pswp__button--share" style="display:none" title="Share"></button>

                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>

                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>

                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                        <div class="pswp__preloader__cut">
                            <div class="pswp__preloader__donut"></div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div>
            </div>

            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>

            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>

            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>

        </div>

    </div>

</div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.css">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/default-skin/default-skin.css">
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/photoswipe@4.1.3/dist/photoswipe-ui-default.min.js"></script>

<script>
    function viewer_init() {
        let pswpElement = document.querySelectorAll('.pswp')[0];
        let $imgArr = document.querySelectorAll(('.article-entry img:not(.reward-img)'))

        $imgArr.forEach(($em, i) => {
            $em.onclick = () => {
                // slider展开状态
                // todo: 这样不好，后面改成状态
                if (document.querySelector('.left-col.show')) return
                let items = []
                $imgArr.forEach(($em2, i2) => {
                    let img = $em2.getAttribute('data-idx', i2)
                    let src = $em2.getAttribute('data-target') || $em2.getAttribute('src')
                    let title = $em2.getAttribute('alt')
                    // 获得原图尺寸
                    const image = new Image()
                    image.src = src
                    items.push({
                        src: src,
                        w: image.width || $em2.width,
                        h: image.height || $em2.height,
                        title: title
                    })
                })
                var gallery = new PhotoSwipe(pswpElement, PhotoSwipeUI_Default, items, {
                    index: parseInt(i)
                });
                gallery.init()
            }
        })
    }
    viewer_init()
</script>



<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
      tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
      }
  });

  MathJax.Hub.Queue(function() {
      var all = MathJax.Hub.getAllJax(), i;
      for(i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
      }
  });
</script>

<script src="https://cdn.jsdelivr.net/npm/mathjax@2.7.6/unpacked/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>


<script type="text/javascript" src="https://js.users.51.la/20544303.js"></script>
  </div>
</body>

</html>